﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for bzBanner
/// </summary>
public class bzBanner
{
	private dbConnection _DB = null;
    private string _TableName = "bzBanner";
    private string _IDType = "_INT";
    private string _IDCol = "BannerID";
    private string _IDColParent = "";
    private string _TableChild = "";

    public struct Enable
    {
        private static int _Enabled = 1;
        public static int Enabled { get { return _Enabled; } }

        private static int _Disabled = 1;
        public static int Disabled { get { return _Disabled; } }
    }
    public struct BannerPosition
    {
        private static string _Home = "1";
        public static string Home { get { return _Home; } }

        private static string _In = "0";
        public static string In { get { return _In; } }
    }
    public bzBanner()
	{
        _DB = new dbConnection();
	}
    public DataTable getTable(string sKey, string sHome, string sEnabled, string sPage, string sLimit,string sType)
    {
        SqlParameter[] p = new SqlParameter[6];
        p[0] = new SqlParameter("@Key", "%" + sKey + "%");
        p[1] = new SqlParameter("@Hot", sHome);
        p[2] = new SqlParameter("@Enabled", sEnabled);
        p[3] = new SqlParameter("@Page", sPage);
        p[4] = new SqlParameter("@Limit", sLimit);
        p[5] = new SqlParameter("@Type", sType);
        return _DB.getDataTable(_TableName + "_search", p);

    }
    public long getTotal(string sKey, string sHome, string sEnabled, string sType)
    {
        SqlParameter[] p = new SqlParameter[4];        
        p[0] = new SqlParameter("@Key", "%" + sKey + "%");
        p[1] = new SqlParameter("@Hot", sHome);
        p[2] = new SqlParameter("@Enabled", sEnabled);
        p[3] = new SqlParameter("@Type", sType);


        return _DB.getTotalTable(_TableName+"_getTotal",p);
    }
    public DataTable getRecord(string ID, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@Enabled", Enabled);
        return _DB.getDataTable("bz_getRecord" + _IDType, p);
    }
    public DataTable getRecordType(string ID, string Type)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@Enabled", Type);
        return _DB.getDataTable("bz_getRecord" + _IDType, p);
    }   
    public int deleteRecord(string[] sID)
    {
        int count = 0;
        foreach (string str in sID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@TableChild", _TableChild);
            p[2] = new SqlParameter("@IDCol", _IDCol);
            p[3] = new SqlParameter("@IDValue", str);
            count += _DB.updateData("bz_deleteRecord" + _IDType, p);
        }
        return count;
    }
    public int activeRecord(string[] ID, string Enabled)
    {
        int count = 0;
        foreach (string str in ID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", str);
            p[3] = new SqlParameter("@Enabled", Enabled);
            count += _DB.updateData("bz_activeRecord" + _IDType, p);
        }
        return count;
    }

    public int saveOrder(string[] ID, string[] Order)
    {
        if (ID.Length != Order.Length) return 0;
        int count = 0;
        for (int i = 0; i < ID.Length; i++)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", ID[i]);
            p[3] = new SqlParameter("@Order", Order[i]);
            count += _DB.updateData("bz_saveOrder" + _IDType, p);
        }
        return count;

    }
    public int upOrder(string ID)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        return _DB.updateData("bz_upOrder" + _IDType, p);
    }
    public int downOrder(string ID)
    {
        SqlParameter[] p = new SqlParameter[3];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        return _DB.updateData("bz_downOrder" + _IDType, p);
    }
    public int insertRecord(string sFileName,string sHome,string sURL,string sEnabled,string sType)
    {
        SqlParameter[] p = new SqlParameter[5];
        p[0] = new SqlParameter("@FileName", sFileName);
        p[1] = new SqlParameter("@Hot", sHome);      
        p[2] = new SqlParameter("@URL", sURL);     
        p[3] = new SqlParameter("@Enabled", sEnabled);
        p[4] = new SqlParameter("@Type", sType);
        return _DB.updateData(_TableName+"_insert",p);
    }   
    
    public int updateRecord(string sID, string sFileName, string sHome, string sURL, string sEnabled,string sType)
    {
        SqlParameter[] p = new SqlParameter[6];
        p[0] = new SqlParameter("@ID", sID);
        p[1] = new SqlParameter("@FileName", sFileName);
        p[2] = new SqlParameter("@Hot", sHome);
        p[3] = new SqlParameter("@URL", sURL);
        p[4] = new SqlParameter("@Enabled", sEnabled);
        p[5] = new SqlParameter("@Type", sType);
        return _DB.updateData(_TableName + "_update", p);
    }
}
